package com.gams.springboot.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.gams.springboot.listener.QuotaListener;
import com.gams.springboot.pojo.Quota;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author tql
 * @current springboot
 * @create 2021-09--8:38
 */
@Controller
public class QuotaController {

    @Autowired
    private QuotaListener quotaListener;

    @RequestMapping(value = "/read",method = RequestMethod.POST)
    @ResponseBody
    public String readExcel(@RequestBody MultipartFile file){

        try {
            /**
             * Build excel the read
             *
             * @param inputStream
             *            Input stream to read.
             *            输入要读取的流
             * @param head
             *            Annotate the class for configuration information.
             *            每行存储的实体类型
             * @param readListener
             *            Read listener.
             *            读监听器，每读一行内容都会调用一次该对象的invoke，在invoke中可以操作读取到的数据
             * @return Excel reader builder.
             */

            //获得工作簿
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), Quota.class, quotaListener);
            //获得工作表  又两种形形式可以通过下标也可以通过名字2003Excel不支持名字
            ExcelReaderSheetBuilder sheet = read.sheet();
            //进行读操作
            sheet.doRead();
            return "ok";
        }catch (Exception e){
            e.printStackTrace();
            return "false";
        }
    }

    @RequestMapping("/write")
    public void writeExcel(HttpServletResponse response) throws IOException {

        //设置文件头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName="hello";
        response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".xlsx\"");

        //把东西按流形式进行保存方便等下进行文件下载功能
        ServletOutputStream outputStream = response.getOutputStream();

        /**
         * Build excel the write
         *
         * @param outputStream
         *            Output stream to write
         *            输出流写入
         * @param head
         *            Annotate the class for configuration information.
         *            每行存储的实体类型
         * @return Excel writer builder
         */
        ExcelWriterBuilder write = EasyExcel.write(outputStream, Quota.class);

        List<Quota> list=new ArrayList<>();

        for (int i=0;i<100;i++){
            Quota user=new Quota();
            user.setName("浪哥"+i+"号");
            user.setGender("男");
            user.setDate(new Date());
            list.add(user);
            System.out.println(i);
        }
        write.sheet().doWrite(list);
    }
}

